# ---
# Fudenberg, Gao, & Liang:
# "How Flexible is that Functional Form? Quantifying the Restrictiveness of Theories"
# 
# Application 3: Microfinance Takeup

# "GenNonlinearData.R"

# Research assistant: Stephanie Nam
# Date: 09/24/2023

# NOTE: This R script generates the variables to be used subsequently in the partially linear model

# ---

library(dplyr)
library(igraph)

sink("GenNonlinearData.log", append=FALSE, split=FALSE)

# Load Village Networks & Find Eigenvector Centralities
villages_eig <- lapply(list.files(path = "adj_all", pattern = "*.csv",
                                  full.names = TRUE), read.csv,
                       header = FALSE) %>%
  lapply(as.matrix) %>%
  lapply(graph.adjacency, mode = "undirected") %>%
  lapply(eigen_centrality, scale = FALSE) %>%
  lapply(function(l) l[[1]])

# Load Leader Status for All Villages
leaders <- lapply(list.files(path = "HHhasALeader", pattern = "*.csv",
                             full.names = TRUE), read.csv, sep = "",
                  header = FALSE) %>%
  lapply(function(l) l[l$V2 == 1,]$V1)

# Load Nonleader Status for All Villages
nonleaders <- lapply(list.files(path = "HHhasALeader", pattern = "*.csv",
                                full.names = TRUE), read.csv, sep = "",
                     header = FALSE) %>%
  lapply(function(l) l[l$V2 == 0,]$V1)

# Load Microfinance Participation Status for All Villages
mf <- lapply(list.files(path = "MF", pattern = "*.csv",
                        full.names = TRUE),
             read.csv, header = FALSE)

# Microfinance Participation Rate of Nonleader Households
mf_rate <- mapply(function(non,m) sum(m[non,])/length(non), non = nonleaders, m = mf)

# Load in the 43 Village Networks
villages <- lapply(list.files(path = "adj_all", pattern = "*.csv",
                              full.names = TRUE), read.csv,
                   header = FALSE) %>%
  lapply(as.matrix) %>%
  lapply(graph.adjacency, mode = "undirected")

networks <- lapply(list.files(path = "adj_all", pattern = "*.csv",
                              full.names = TRUE), read.csv,
                   header = FALSE) %>% lapply(as.matrix)

T_i <- villages %>% sapply(diameter, unconnected=TRUE) 

# Save to a RData file
save(villages, villages_eig, networks, nonleaders, leaders, mf_rate, T_i,
     file = "NetNonlinearData.RData")

sink()
